/** 
 * File-Name:ImportOldEST.java
 *
 * Created on 2011-10-12 下午12:28:22
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package com.debugassistant.service.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.debugassistant.dao.ESTRepository;
import com.debugassistant.entity.EST;
import com.debugassistant.service.est.ESTConstructor;

/**
 * Description: Import old EST crawled from the Internet.
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-10-12 下午12:28:22
 */
public class ImportOldEST {
	private static String DB_URL = "jdbc:mysql://192.168.4.181:3307/exceptionhandlerall";
	private static String DB_USER = "root";
	private static String DB_PWD = "woxnsk";
	private static String SQL_SELECT_OLD_ETS = "select distinct stackTraceString, url, reserve03 from exceptioninfo";

	public static void importFromOldDatabase() {
		Connection connection = null;
		// 加载驱动程序以连接数据库
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);

			PreparedStatement preparedStatement = connection
					.prepareStatement(SQL_SELECT_OLD_ETS);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String estString = resultSet.getString("stackTraceString");
				String url = resultSet.getString("url");
				String project = resultSet.getString("reserve03");
				if (estString != null && !"".equals(estString)) {
					EST est = ESTConstructor.construct(estString);
					est.setUrl(url);
					est.setReserve01(project);
					ESTRepository.save(est);
				}
			}
			preparedStatement.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}

	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		ImportOldEST.importFromOldDatabase();
	}

}
